package com.streamsets.common.model;


import java.io.Serializable;

/**
 * 通用响应Bean
 *
 **/

public class ResponseData<T>  implements Serializable {

    private static final long serialVersionUID = 6435253525531549949L;
    /**
     * 成功的默认状态码
     */
    public static final String DEFAULT_SUCCESS_CODE = ResultCodeEnum.SUCCESS.getCode();
    /**
     * 成功的默认描述
     */
    public static final String DEFAULT_SUCCESS_MESSAGE = ResultCodeEnum.SUCCESS.getMessage();

    /**
     *  是否请求成功，1成功0失败
     */
    private Integer success;

    /**
     * 业务错误码
     */
    private String code;

    /**
     * 描述
     */
    private String message;

    /**
     * 结果集
     */
    private T data;


    public ResponseData() {
    }


    /**
     * 返回是否成功
     * @return
     */
    public  boolean isSuccess(){

       return (this.getCode()!=null && this.getCode().equals(DEFAULT_SUCCESS_CODE));
    }

    /**
     * 返回成功的响应对象
     * @param 
     * @return
     */
    public static  ResponseData success() {
        return restResult(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, null,1);
    }

    /**
     * 根据结果集对象，返回成功的响应对象
     * @param data
     * @param 
     * @return
     */
    public static <T> ResponseData<T> success(T data) {
        return restResult(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, data,1);
    }

    /**
     * 根据错误消息描述，返回失败的响应对象
     * @param 
     * @return
     */
    public static  ResponseData failed() {
        return restResult(ResultCodeEnum.SERVICE_ERROR.getCode(), ResultCodeEnum.SERVICE_ERROR.getMessage(), null,0);
    }

    /**
     * 根据错误消息描述，返回失败的响应对象
     * @param message 消息描述
     * @param 
     * @return
     */
    public static  ResponseData failed(String message) {
        return restResult(ResultCodeEnum.SERVICE_ERROR.getCode(), message, null,0);
    }
    

    /**
     * 根据状态码和消息描述，返回失败的响应对象
     * @param code
     * @param message
     * @param 
     * @return
     */
    public static  ResponseData failed(String code, String message) {
        return restResult(code, message, null,0);
    }

    /**
     * 根据错误响应码、消息描述和结果集，返回失败的响应对象
     * @param code
     * @param message
     * @param data
     * @param 
     * @return
     */
    public static <T>  ResponseData<T> failed(String code, String message, T data) {
        return restResult(code, message, data,0);
    }


    public ResponseData(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 根据状态码、消息描述和结果集，返回响应对象
     * @param code
     * @param message
     * @param data
     * @param 
     * @return
     */
    private static <T> ResponseData<T> restResult(String code, String message, T data,Integer success) {
        ResponseData result = new ResponseData();
        result.setSuccess(success);
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    public static  ResponseData.ResultDataBuilder builder() {
        return new ResponseData.ResultDataBuilder();
    }

    /*
     * 验名错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfSignError() {
        return restResult(ResultCodeEnum.SIGN_ERROR.getCode(), ResultCodeEnum.SIGN_ERROR.getMessage(), null ,0);
    }

    /*
     * 参数校验错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfParamError(String message) {
        if(message==null || "".equals(message.trim())){
            message = ResultCodeEnum.PARAM_VALID_FAIL.getMessage();
        }
        return restResult(ResultCodeEnum.PARAM_VALID_FAIL.getCode(), message, null ,0);
    }

    /*
     * 参数校验错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfParamError() {
        return restResult(ResultCodeEnum.PARAM_VALID_FAIL.getCode(), ResultCodeEnum.PARAM_VALID_FAIL.getMessage(), null, 0);
    }

    /*
     * 参数校验错误
     * @param [message]
     * @return
     */
    public static <T>  ResponseData<T> failedOfParamError(T data) {
        return restResult(ResultCodeEnum.PARAM_VALID_FAIL.getCode(), ResultCodeEnum.PARAM_VALID_FAIL.getMessage(), data , 0);
    }
    /*
     * Token校验错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfTokenError(String message) {
        if(message==null || "".equals(message.trim())){
            message = ResultCodeEnum.TOKEN_ERROR.getMessage();
        }
        return restResult(ResultCodeEnum.TOKEN_ERROR.getCode(), message, null ,0);
    }

    /*
     * Token校验错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfTokenError() {
        return restResult(ResultCodeEnum.TOKEN_ERROR.getCode(), ResultCodeEnum.TOKEN_ERROR.getMessage(), null ,0);
    }

    /*
     * 权限校验错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfPermission(String message) {
        if(message==null || "".equals(message.trim())){
            message = ResultCodeEnum.PERMISSION_ERROR.getMessage();
        }
        return restResult(ResultCodeEnum.PERMISSION_ERROR.getCode(), message, null ,0);
    }

    /*
     * 权限校验错误
     * @param [message]
     * @return
     */
    public static  ResponseData failedOfPermission() {
        return restResult(ResultCodeEnum.PERMISSION_ERROR.getCode(), ResultCodeEnum.PERMISSION_ERROR.getMessage(), null ,0);
    }



    public static class ResultDataBuilder {
        private String code;
        private String msg;
        private Object data;

        ResultDataBuilder() {
        }

        public ResponseData.ResultDataBuilder code(String code) {
            this.code = code;
            return this;
        }

        public ResponseData.ResultDataBuilder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public <T> ResponseData.ResultDataBuilder data(T data) {
            this.data = data;
            return this;
        }

        public ResponseData build() {
            return new ResponseData(this.code, this.msg, this.data);
        }

        public String toString() {
            return "ResponseData.ResultDataBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ")";
        }
    }

    public Integer getSuccess() {
      return success;
    }

    public void setSuccess(Integer success) {
      this.success = success;
    }

    public String getCode() {
      return code;
    }

    public void setCode(String code) {
      this.code = code;
    }

    public String getMessage() {
      return message;
    }

    public void setMessage(String message) {
      this.message = message;
    }

    public T getData() {
      return data;
    }

    public void setData(T data) {
      this.data = data;
    }
}
